import { _decorator, Component, Node, v3 ,ColliderComponent, Collider2D, Contact2DType, IPhysics2DContact, isValid, Animation, Prefab, instantiate, director} from 'cc';
import { Bullet } from './Bullet';
import { Plant } from './Plant';
import { Main } from './Main';
import { AudioController } from './AudioController';
const { ccclass, property } = _decorator;
import { GameData, GameState } from './GameData';

const gameData = GameData.getInstance()

@ccclass('Zombie')
export class Zombie extends Component {
    speed = 30;

    isAttacting = false // 是否在攻击植物

    isDead = false // 是否死了

    health: number = 100; // 僵尸的生命值

    animation = null

    attackInterval = 2 // 2s攻击一次

    attackDamage = 20 // 每次攻击的伤害

    @property(Prefab)
    hurtEffect = null; // 伤害效果

    scheduleAttack = null

    audioController = null

    start() {
        const collider = this.getComponent(Collider2D);
        if (collider) {
            collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
        }
        this.animation = this.getComponent(Animation);

        this.audioController = director.getScene().getChildByName('Canvas').getComponent(AudioController);

    }

    update(deltaTime: number) {
        if(this.isAttacting || this.isDead || gameData.gameSate !== GameState.ready){
            
        }else{
            this.move(deltaTime)
        }
    }
    move(deltaTime){
        this.node.translate(v3(-this.speed * deltaTime, 0, 0));
    }

    onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact) {
        // 检测到与子弹的碰撞
        if (otherCollider.node.name === 'bullet') {
            
            this.audioController.playOneShot('enemyAttacked')
            // 减少僵尸生命值或执行其他逻辑
            this.health -= otherCollider.node.getComponent(Bullet).damage;
            if (this.health <= 0) {
                // 如果生命值小于等于0，销毁僵尸节点

                this.scheduleOnce(() => {
                    this.audioController.playOneShot('enemyDie')
                    this.node.destroy();
                }, 0)
            }
            const hurtEffect = instantiate(this.hurtEffect);
            hurtEffect.parent = selfCollider.node;
            const worldManifold = contact.getWorldManifold();
            const points = worldManifold.points;
            // 销毁子弹节点
            this.scheduleOnce(() => {
                otherCollider.node.destroy();
            }, 0);
            this.scheduleOnce(() => {
                hurtEffect.destroy();
            }, 100)
        }else if(otherCollider.node.name === 'endLine'){
            // 终点碰撞
            director.getScene().getChildByName('Canvas').getComponent(Main).setGameOver();
            this.audioController.playOneShot('gameOverClip')
        }else {
            // 碰撞到植物 正在攻击
            if(this.isAttacting){
                return;
            }
            this.isAttacting = true;
            this.animation.play('zombies_attack');
            this.scheduleAttack = () => {
                if(gameData.gameSate !== GameState.ready) return;
                this.audioController.playOneShot('plantAttackedClip')
                this.attack(otherCollider.node);
            }
            this.schedule(this.scheduleAttack, this.attackInterval)
        }
    }
    attack(plant){
        // 要多个僵尸同时攻击一个植物，植物已经被杀死
        if(!plant){
            this.unschedule(this.scheduleAttack) // 取消定时器
            this.isAttacting = false;
            this.animation.play('zombies_walk')
            return
        }
        // 僵尸攻击植物，如果植物被杀了，重新往前走
        plant.getComponent(Plant).onAttacked(this.attackDamage, () => {
            this.unschedule(this.scheduleAttack) // 取消定时器
            this.isAttacting = false;
            this.animation.play('zombies_walk')
        })
    }
}


